﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;

    public class Dropslider extends Sprite
    {
        private var min:int = 0;
        private var max:int = 100;
        private var tb:NumberInput;
        private var slider:Slider;
        private var click:IconButton;
        private var holder:Sprite;
        private var disable:Sprite;
        private var dropShadow:DropShadowFilter;

        public function Dropslider(param1:int, param2:int, param3:int, param4:int = 100)
        {
            this.dropShadow = new DropShadowFilter(2, 45, 0, 0.2, 3, 3, 1, 2);
            this.min = param2;
            this.max = param3;
            this.holder = new Sprite();
            this.click = new IconButton(null, "", new DropdownOver());
            this.tb = new NumberInput(param1 - this.click.width, param4, param2, param3);
            this.tb.addEventListener(NumberEvent.CHANGE, this.NumberChange, false, 1, true);
            addChild(this.tb);
            this.click.addEventListener(MouseEvent.MOUSE_DOWN, this.ClickMouseDown, false, 0, true);
            this.click.x = this.tb.x + this.tb.width;
            addChild(this.click);
            this.DrawSlider(param4);
            return;
        }// end function

        public function Disable(disable:Boolean = true) : void
        {
            if (this.disable == null)
            {
                this.disable = new Sprite();
                this.disable.visible = false;
                this.disable.alpha = 0.3;
                this.disable.graphics.beginFill(9474192);
                this.disable.graphics.drawRect(0, 0, 10, 10);
                this.disable.graphics.endFill();
                this.disable.height = this.tb.height;
                this.disable.width = this.tb.width + this.click.width;
                addChild(this.disable);
            }
            this.tb.t.type = disable ? (TextFieldType.DYNAMIC) : (TextFieldType.INPUT);
            this.disable.visible = disable;
            return;
        }// end function

        public function Set(disable:int) : void
        {
            this.slider.Set(disable / this.max, false);
            this.tb.value = disable;
            return;
        }// end function

        private function DrawSlider(disable:int) : void
        {
            this.holder.visible = false;
            this.holder.filters = [this.dropShadow];
            this.holder.x = this.click.x - 96;
            this.holder.y = 18;
            this.holder.graphics.beginFill(9474192);
            this.holder.graphics.drawRect(0, 0, 117, 23);
            this.holder.graphics.beginFill(13225424);
            this.holder.graphics.drawRect(1, 1, 115, 21);
            this.holder.graphics.endFill();
            this.slider = new Slider(100);
            this.slider.addEventListener(SliderEvent.DRAG, this.SliderDrag, false, 1, true);
            this.slider.addEventListener(SliderEvent.RELEASE, this.SliderRelease, false, 1, true);
            this.slider.x = 8;
            this.slider.y = 1;
            this.slider.Set(this.tb.value / this.max);
            this.holder.addChild(this.slider);
            addChild(this.holder);
            return;
        }// end function

        function NumberChange(event:NumberEvent) : void
        {
            this.slider.Set(event.val / this.max);
            dispatchEvent(new SliderEvent(SliderEvent.DRAG, event.val));
            dispatchEvent(new SliderEvent(SliderEvent.CHANGE, event.val));
            return;
        }// end function

        function SliderDrag(event:SliderEvent) : void
        {
            if (!event.manual)
            {
                this.tb.value = Math.round(event.percent * (this.max - this.min) + this.min);
                dispatchEvent(new SliderEvent(SliderEvent.DRAG));
            }
            else
            {
                dispatchEvent(new SliderEvent(SliderEvent.DRAG));
                dispatchEvent(new SliderEvent(SliderEvent.CHANGE));
            }
            return;
        }// end function

        function SliderRelease(event:SliderEvent) : void
        {
            dispatchEvent(new SliderEvent(SliderEvent.CHANGE));
            return;
        }// end function

        private function ClickMouseDown(event:MouseEvent) : void
        {
            this.holder.visible = !this.holder.visible;
            if (this.holder.visible)
            {
                stage.addEventListener(MouseEvent.MOUSE_DOWN, this.ClickOutside, true);
            }
            else
            {
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.ClickOutside, true);
            }
            return;
        }// end function

        private function ClickOutside(event:MouseEvent) : void
        {
            if (!this.holder.hitTestPoint(event.stageX, event.stageY) && !this.click.hitTestPoint(event.stageX, event.stageY))
            {
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.ClickOutside, true);
                this.holder.visible = false;
            }
            return;
        }// end function

        public function get value() : int
        {
            return this.tb.value;
        }// end function

    }
}
